﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class FormDanhSachLoaiMonAn : Form
    {
        public FormDanhSachLoaiMonAn()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormDanhSachLoaiMonAn_Load(object sender, EventArgs e)
        {
            List<LoaiMonAnDTO> danhsach = new List<LoaiMonAnDTO>();
            LoaiMonAnBUS bus = new LoaiMonAnBUS();
            danhsach = bus.LayDanhSachLoaiMonAn();
            gridViewXemDanhSachLoaiMonAn.Rows.Clear();
            for (int i = 0; i < danhsach.Count; i++)
            {
                gridViewXemDanhSachLoaiMonAn.Rows.Add(i+1,danhsach[i].Id_Loai, danhsach[i].Ten_Mon_An);
            }
        }

        private void chkChonTatCa_CheckedChanged(object sender, EventArgs e)
        {

            if (chkChonTatCa.Checked)
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachLoaiMonAn.Rows)
                {
                    r.Cells[3].Value = true;
                }
            }
            else if (!(chkChonTatCa.Checked))
            {
                foreach (DataGridViewRow r in gridViewXemDanhSachLoaiMonAn.Rows)
                {
                    r.Cells[3].Value = false;
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult dialog = MessageBox.Show("Bạn Chắc Chắn Muốn Xóa Loại Món Ăn Này?", "Xóa Xóa Loại Món Ăn", MessageBoxButtons.YesNo);
            if (dialog == DialogResult.Yes)
            {
                List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>();
                foreach (DataGridViewRow row in gridViewXemDanhSachLoaiMonAn.Rows)
                {
                    DataGridViewCheckBoxCell chk = row.Cells[3] as DataGridViewCheckBoxCell;
                    if (Convert.ToBoolean(chk.Value) == true)
                        rowsToDelete.Add(row);
                }
                //loop through the list to delete rows added to list<T>:
                foreach (DataGridViewRow row in rowsToDelete)
                {
                    LoaiMonAnBUS tk = new LoaiMonAnBUS();
                    tk.XoaLoaiMonAn(Convert.ToInt32(row.Cells[1].Value));
                    gridViewXemDanhSachLoaiMonAn.Rows.Remove(row);
                }
            }  
        }

        private void btnThemLoaiMon_Click(object sender, EventArgs e)
        {
            FormThemLoaiMonAn frm = new FormThemLoaiMonAn();
            frm.Show();
        }
        public static int ID;
        public static string Ten_Loai_Mon = "";
        private void btnSua_Click(object sender, EventArgs e)
        {
            int chkDem = 0;
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachLoaiMonAn.Rows)
            {
                if (singleRow.Cells[3].Value != null)
                {
                    if ((Boolean)singleRow.Cells[3].Value == true)
                    {
                        chkDem = chkDem + 1;
                    }
                }
            }
            if (chkDem == 0 || chkDem > 1)
            {
                MessageBox.Show("Chỉ Được Chọn 1 Loại Món Ăn Để Tiến Hành Sửa Đổi!");
                return;
            }
            foreach (DataGridViewRow singleRow in gridViewXemDanhSachLoaiMonAn.Rows)
            {
                if (singleRow.Cells[3].Value != null)
                {
                    if ((Boolean)singleRow.Cells[3].Value == true)
                    {
                        ID = Convert.ToInt32(singleRow.Cells[1].Value.ToString());
                        Ten_Loai_Mon = singleRow.Cells[2].Value.ToString();
                    }
                }
            }
            FormCapNhatLoaiMonAn f = new FormCapNhatLoaiMonAn();
            f.Show();
        }
    }
}
